from flask import Flask
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from config import Config
from .models import db
from .auth import auth as auth_blueprint  # 确保这行存在

jwt = JWTManager()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)
    
    # 初始化扩展
    db.init_app(app)
    CORS(app, supports_credentials=True)
    jwt.init_app(app)
    
    # 注册蓝图
    from .routes import main as main_blueprint
    app.register_blueprint(main_blueprint)  # 移除 url_prefix

    # 确保 auth Blueprint 正确注册
    app.register_blueprint(auth_blueprint, url_prefix='/api/auth')
    
    # 创建数据库表
    with app.app_context():
        db.create_all()
        
    return app